package com.example.Controller;

import com.example.dao.RecordDao;
import com.example.dao.ToupiaoDao;
import com.example.dao.UserDao;
import com.example.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Controller
public class borrow {
    public String name;
    @Autowired
    UserDao userDao;
    @Autowired
    ToupiaoDao toupiaoDao;
    @Autowired
    RecordDao recordDao;
    public String code;
    int flag;
    public void setz() {
        List<Toupiao> all = toupiaoDao.findAll();
        for (Toupiao toupiao : all) {
            toupiao.setPiao("0");
            toupiaoDao.update(toupiao);
        }
    }

    @RequestMapping("/borrow")
    public String showBorrowBook(Model model) {
        if (flag == 0) {
            setz();
        }
        List<Toupiao> all = toupiaoDao.findAll();
        List<emp> emps = new ArrayList<>();
        List<group> group = recordDao.group();
        String[] stock = new String[all.size()];
        System.out.println("数量为"+all.size());
        int n = 0;
        for (Toupiao toupiao : all) {
            stock[n] = toupiao.getNumber();
            n++;
            emps.add(new emp(toupiao.getId(), toupiao.getName(), "0", toupiao.getPrice()));
        }
        String[] nums = new String[group.size()];
        for (int i = 0; i < group.size(); i++) {
            nums[i] = group.get(i).getSum();
        }
        int m = 0;
        for (emp emp : emps) {
            System.out.println(emp.toString());
            if (nums.length<=   m){
                emp.setSum(String.valueOf(Integer.parseInt(stock[m])));
                System.out.println();
            }
            else {
                emp.setSum(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                System.out.println(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                m++;
            }
        }
        for (emp emp : emps) {
             emp.setPrice(emp.getPrice()/10);
        }
        model.addAttribute("emps", emps);
        Collections.sort(emps);
        flag++;
        return "/borrow/showBorrowBook";
    }
    @RequestMapping("willBorrowBook")
    public String willBorrow(Model model){
        if (flag == 0) {
            setz();
        }
        List<Toupiao> all = toupiaoDao.findAll();
        double AllPrice = 0;
        for (Toupiao toupiao : all) {
            AllPrice = AllPrice + toupiaoDao.findPrice(toupiao.getId()) * Integer.parseInt(toupiao.getPiao());
        }
        String s = String.valueOf(AllPrice/10);
        s = "您的租金为" + s +"元";
        model.addAttribute("msg", s);
        Collections.sort(all);
        model.addAttribute("emps",all);
        return "/borrow/willBorrowBook";
    }

    @GetMapping("/borrow/{id}")
    public String borrowById(@PathVariable("id") String id, Model model) {
        if (id.equals("0"))
        {
            List<Toupiao> all = toupiaoDao.findAll();
            Collections.sort(all);
            model.addAttribute("emps",all);
            return "/borrow/willBorrowBook";
        }
        else {
            Toupiao toupiao = toupiaoDao.find(id);
            Integer integer = Integer.parseInt(toupiao.getPiao()) + 1;
            String s = String.valueOf(integer);
            toupiao.setPiao(s);
            toupiaoDao.update(toupiao);
            List<Toupiao> all = toupiaoDao.findAll();
            List<emp> emps = new ArrayList<>();
            List<group> group = recordDao.group();
            String[] stock = new String[all.size()];
            int n = 0;
            for (Toupiao tou : all) {
                stock[n] = tou.getNumber();
                n++;
                emps.add(new emp(tou.getId(), tou.getName(), "0", tou.getPrice()));
            }
            String[] nums = new String[group.size()];
            for (int i = 0; i < group.size(); i++) {
                nums[i] = group.get(i).getSum();
            }
            int m = 0;
            for (emp emp : emps) {
                emp.setSum(String.valueOf(Integer.parseInt(stock[m]) - Integer.parseInt(nums[m])));
                m++;
            }
            for (emp emp : emps) {
                emp.setPrice(emp.getPrice()/10);
            }
            model.addAttribute("emps", emps);
            Collections.sort(emps);
            return "/borrow/showBorrowBook";
        }
    }
}



